package com.liang.paul.officelite.fragments;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gitonway.lee.niftymodaldialogeffects.lib.Effectstype;
import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;
import com.liang.paul.officelite.R;
import com.liang.paul.officelite.activities.AddTeamActivity;
import com.liang.paul.officelite.activities.CreateTeamActivity;
import com.liang.paul.officelite.adapters.MemberAdpter;
import com.liang.paul.officelite.beams.OfficeUser;
import com.liang.paul.officelite.beams.TeamBeam;
import com.liang.paul.officelite.beams.TeamPersonMapBeam;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.GetListener;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainPageFragment extends Fragment {

    View rootview;
    String teamId;
    TextView name, date, id;
    ListView memberlist;
    ArrayList<OfficeUser> datas;
    MemberAdpter adpter;

    public MainPageFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootview = inflater.inflate(R.layout.fragment_main_page, container, false);
        initialView();
        return rootview;
    }

    private void initialView(){
        teamId = getArguments().getString("teamId");
        id = (TextView) rootview.findViewById(R.id.txt_main_id);
        name = (TextView) rootview.findViewById(R.id.txt_main_name);
        date = (TextView) rootview.findViewById(R.id.txt_main_createat);
        memberlist = (ListView) rootview.findViewById(R.id.list_main_memberlist);


        datas = new ArrayList<OfficeUser>();
        adpter = new MemberAdpter(getActivity(), datas);
        memberlist.setAdapter(adpter);

        getData();

        memberlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                NiftyDialogBuilder builder = NiftyDialogBuilder.getInstance(getActivity());
                builder
                        .withTitle("选择操作")
                        .withTitleColor("#FFFFFF")
                        .withDividerColor("#11000000")
                        .withMessage("")
                        .withDialogColor("#666666")
                        .withEffect(Effectstype.Fall)
                        .withButton1Text("呼叫")
                        .withButton2Text("短信")
                        .setButton1Click(new View.OnClickListener(){
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(Intent.ACTION_CALL,
                                        Uri.parse("tel:" + datas.get(position).getMobilePhoneNumber()));
                                startActivity(intent);
                            }
                        })
                        .setButton2Click(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(Intent.ACTION_SENDTO,
                                        Uri.parse("smsto:" + datas.get(position).getMobilePhoneNumber()));
                                startActivity(intent);
                            }
                        })
                        .show();
            }
        });
    }

    private void getData(){
        getTeamData();
        getMemberData();
    }

    private void getTeamData(){
        id.setText("团队唯一ID: " + teamId);

        BmobQuery<TeamBeam> query = new BmobQuery<TeamBeam>();
        query.getObject(getActivity(), teamId, new GetListener<TeamBeam>() {
            @Override
            public void onSuccess(TeamBeam teamBeam) {
                name.setText(teamBeam.getName());
                date.setText("创建日期: " + teamBeam.getCreatedAt());
            }

            @Override
            public void onFailure(int i, String s) {
                Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getMemberData(){
        BmobQuery<TeamPersonMapBeam> query = new BmobQuery<TeamPersonMapBeam>();
        query.addWhereEqualTo("teamId", teamId);
        query.setLimit(50);
        query.findObjects(getActivity(), new FindListener<TeamPersonMapBeam>() {
            @Override
            public void onSuccess(List<TeamPersonMapBeam> list) {
                for(TeamPersonMapBeam beam : list){
                    BmobQuery<OfficeUser> userBmobQuery = new BmobQuery<OfficeUser>();
                    userBmobQuery.getObject(getActivity(), beam.getPersonId(), new GetListener<OfficeUser>() {
                        @Override
                        public void onSuccess(OfficeUser officeUser) {
                            datas.add(officeUser);
                            adpter.notifyDataSetChanged();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }

            @Override
            public void onError(int i, String s) {
                Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
